package controllers;

import helpers.UrlHelper;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.concurrent.Future;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

@SuppressWarnings("serial")
public class CheckOrderKeywordServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String keyword = (String) request.getParameter("keyword");
		String type = (String) request.getParameter("type");
		String name = (String) request.getParameter("name");
		List<String> sites = new ArrayList<String>();
		List<String> order = new ArrayList<String>();
		List<String> pages = new ArrayList<String>();
		
		//String url = "http://" + type + "/search?hl=en&ie=UTF-8&oe=UTF-8&q=" + keyword + "&btnG=Google+Search&as_qdr=d";
		
		if (keyword != null && name != null && UrlHelper.checkDomain(name)) {
			List<Future> arFuture = new ArrayList<Future>();
			URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
			
			for (int i = 0; i < 10; i++) {
				URL url = new URL("http://" + type + "/search?hl=en&ie=UTF-8&oe=UTF-8&q=" + URLEncoder.encode(keyword) + "&btnG=Google+Search&as_qdr=d&start="+i*10);			
				arFuture.add(fetcher.fetchAsync(url));
			}
			
			for (int i = 0; i < arFuture.size(); i++) {
				Future future = arFuture.get(i);
				try {
					HTTPResponse response2 = (HTTPResponse) future.get();
		            
		            String content = this.getContentCharset(response2);
		            
		            Document doc = Jsoup.parse(content);
		            
		            Elements elms = doc.select("#search #ires ol li h3.r a");
		            for (int j = 0; j < elms.size(); j++) {
		            	String ahref = elms.get(j).attr("href").replace("/url?q=", "");
		            	String domain = UrlHelper.fomatDomains(ahref);
		            	if (UrlHelper.checkDomain(domain) && domain.contains(name)) {
		            		sites.add(domain);
		            		order.add(Integer.toString(j+1));
		            		pages.add(Integer.toString(i+1));
		            	}
		            }
		            
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		
		//Cache header
		response.addHeader("Cache-Control", "public, max-age=60000");
		
		request.setAttribute("type", type);
		request.setAttribute("name", name);
		request.setAttribute("keyword", keyword);
		request.setAttribute("sites", sites);
		request.setAttribute("order", order);
		request.setAttribute("pages", pages);
		try {
			request.getRequestDispatcher("/pages/check_order_keyword.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	/********************************************* LIBRARY FUNCTION *********************************************/
	private String getContentCharset(HTTPResponse response) {
        String charset = "UTF-8";
        String content = "";
        try {
			content = new String(response.getContent(),charset);
			Document doc = Jsoup.parse(content);
	        
	        int index = doc.select("meta").toString().toLowerCase().indexOf("charset=");
	        if (index != -1) {
	        	charset = doc.select("meta").toString().substring(index + 8).replaceAll("[^\\'\\\"0-9a-zA-Z\\-\\_]+[\\w\\W]+", "").replaceAll("[\\'\\\"]+", "");
	        }
	        content = new String(response.getContent(),charset);
	        
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
        
    	return content;
    }
}
